﻿//
// Copyright(C) MixModes Inc. 2011
// 

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MixModes.Synergy.Web.VisualFramework.Controls
{
    /// <summary>
    /// Lays out children sideways
    /// </summary>
    public class SidewaysStackPanel : Panel
    {
        protected override Size MeasureOverride(Size availableSize)
        {
            double width = 0;
            double height = 0;
            foreach (var child in Children)
            {
                child.Measure(new Size(Double.PositiveInfinity, availableSize.Width));
                height += child.DesiredSize.Width;
                width = Math.Max(width, child.DesiredSize.Height);
            }
            return new Size(width, height);
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            double y = 0;
            foreach (var child in Children)
            {
                Rect arrangeRect = new Rect();
                arrangeRect.X = 0;
                arrangeRect.Y = y;
                arrangeRect.Width = child.DesiredSize.Width;
                arrangeRect.Height = finalSize.Width;
                child.Arrange(arrangeRect);
                y += child.DesiredSize.Width;

                RotateTransform rotateTransform = new RotateTransform
                {
                    Angle = 90,
                };

                TranslateTransform translateTransform = new TranslateTransform
                {
                    X = finalSize.Width
                };

                TransformGroup transformGroup = new TransformGroup();
                transformGroup.Children.Add(rotateTransform);
                transformGroup.Children.Add(translateTransform);

                child.RenderTransform = transformGroup;
            }
            return finalSize;
        }

    }
}
